<template>
  <el-card class="menus">
    <!-- 添加按钮 -->
    <el-row type="flex" justify="end">
      <el-button
        type="primary"
        icon="el-icon-edit"
        @click="$refs.addMenus.dialogFormVisible = true"
      >添加</el-button>
    </el-row>
    <!-- 表格 -->
    <el-table
      :data="menuslist"
      style="width: 100%"
      :header-cell-style="{ background: '#fafafa' }"
      row-key="id"
      default-expand-all
      :expand-on-click-node="false"
      :tree-props="defaultProps"
    >
      >
      <el-table-column label="标题" prop="title" />
      <el-table-column label="权限点代码" width="700" prop="code" />
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="{ row }">
          <el-button
            size="mini"
            type="primary"
            @click="handleEdit(row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(row.id)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加修改弹框 -->
    <add-menus ref="addMenus" :editid="editid" :menuslist="tranData" @updata="getmenuslist" />
  </el-card>

</template>
<script>
import { getmenuslist, delmenus, menusdetail } from '@/api/base/menus'
import AddMenus from './add-menus.vue'
export default {
  components: { AddMenus },
  props: {},
  data() {
    return {
      menuslist: [],
      defaultProps: {
        children: 'childs',
        label: 'title'
      },
      tranData: [],
      editid: 0
    }
  },
  computed: {
  },
  watch: {},
  mounted() {},
  created() {
    this.getmenuslist()
  },
  methods: {
    // 获取菜单列表
    async getmenuslist() {
      const { data } = await getmenuslist()
      this.tranData = data
      this.menuslist = JSON.parse(JSON.stringify(data).replace(/points/g, 'childs'))
      // this.menuslist = data
    },
    // disabledData() {
    //   this.tranData.forEach(item => {
    //     item.disabled = true
    //   })
    //   console.log(this.tranData)
    // },
    // 编辑
    async handleEdit(row) {
      try {
        const { data } = await menusdetail(row)
        this.editid = data.id

        this.menuslist.filter(item => {
          if (item.id === this.editid) {
            // console.log(item)
            data.id = item.title
          } else {
            console.log(item.childs)
            if (Array.isArray(item.childs)) {
              item.childs.forEach((items) => {
                if (items.id === this.editid) {
                // console.log(items)
                  data.id = item.title
                  console.log(data.id)
                }
              })
            }
          }
        })
        this.$refs.addMenus.dialogFormVisible = true
        this.$refs.addMenus.addmenussForm = data
      } catch (error) {
        console.log(error)
      }
    },
    // 删除
    async handleDelete(id) {
      try {
        await this.$confirm('此操作将永久删除用户 , 是否继续?？', '提示', {
          type: 'warning'
        })
        await delmenus(id)
        this.getmenuslist()
        this.$message.success('删除成功')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style scoped lang="scss">

</style>

